import React from 'react';
import { WeaTop, WeaInput, WeaTools, WeaLocaleProvider } from 'ecCom';
import { Tabs, Button, message } from 'antd';
import './../../style/scanWorkflow.less'
const TabPane = Tabs.TabPane;
const getLabel = WeaLocaleProvider.getLabel;
const Base64 = WeaTools.Base64;

export default class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            inputValue: "",
            nowTab:"1",
            title:getLabel(127765, "扫码查询")
        }
    }
    componentDidMount(){
        this.onFocus();
    }

    onFocus=()=>{
        setTimeout(()=>{
            if(this.state.nowTab == 2)
                document.getElementById("scan_textarea").focus();
            else{
                this.refs.WeaInput.refs.inputNormal.refs.input.refs.input.focus();
            }
        },0)
        
    }
    onClick = (e) => {
        let requestids = document.getElementById("scan_textarea").value;
        if (requestids === "") {
            message.warning(getLabel(501165, "没有流程可以打开！"))
        } else {
            let arr = requestids.replace(/\n/g, ",").split(",");
            arr.map(item => {
                item && this.checkReqeustId(item).then(datas => {
                    const { isExist = false, isTest = false } = datas;
                    if (isTest) {
                        message.warning(getLabel(501125, "您无权限查看该流程表单"), 2);
                    } else {
                        window.open('/workflow/request/ViewRequestForwardSPA.jsp?requestid=' + item, '_blank')
                    }
                })
            })
        }
    }

    checkReqeustId = (requestid) => {
        return WeaTools.callApi('/api/workflow/reqform/judgeRequestIsValid', 'POST', { requestid: requestid });
    }

    onTabClick=(res)=>{
        this.setState({nowTab:res});
        this.onFocus();
    }

    render() {
        const { inputValue,nowTab } = this.state;
        const { location } = this.props;
        if(location.query.tabTitle){
            this.state.title = Base64.decode(decodeURIComponent(location.query.tabTitle));
        }
        return (
            <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@nz2rsa`}
                title={<span >{this.state.title}</span>}
                icon={<i className='icon-portal-workflow' />}
                iconBgcolor='#0079DE'
            >
                <div className="wf-scanworkflow-content">
                    <div className="wf-scanworkflow-title" >{getLabel(127763, "请使用扫码枪扫描二维码或条形码")}</div>
                    <div className="wf-scanworkflow-subTitle">{getLabel(501170, "注：系统只能识别对应流程ID的二维码/条形码")}</div>
                    <div className="wf-scanworkflow-scanCode">
                        <img src="/images/ecology9/workflow/codedome.png" />
                    </div>
                    <Tabs ecId={`${this && this.props && this.props.ecId || ''}_Tabs@o8kw7r`} type="card"activeKey={nowTab} onTabClick={this.onTabClick}>
                        <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@geo3he`} tab={getLabel(127761, "逐个处理")} key="1">
                            <div className="wf-scanworkflow-tab-content-wrap">
                                <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@7qq8ik`}
                                    onChange={(value) => {
                                        this.setState({ inputValue: value })
                                    }}
                                    value={inputValue}
                                    ref="WeaInput"
                                    onKeyDown={(e) => {
                                        if (e.keyCode && e.keyCode === 13) {
                                            let value = e.target.value;
                                            if (value === "") {
                                                message.warning(getLabel(501165, "没有流程可以打开！"))
                                            } else {
                                                this.checkReqeustId(value).then(datas => {
                                                    const { isExist = false, isTest = false } = datas;
                                                    if (isTest) {
                                                        message.warning(getLabel(501125, "您无权限查看该流程表单"), 2);
                                                    } else {
                                                        window.open('/workflow/request/ViewRequestForwardSPA.jsp?requestid=' + value, '_blank')
                                                        this.setState({ inputValue: "" })
                                                    }

                                                })
                                            }
                                        }
                                    }}
                                />
                            </div>
                            <div className="wf-scanworkflow-tip">{getLabel(127760, "使用扫描枪扫描二维码或条形码可自动打开条码对应的流程")}</div>
                        </TabPane>
                        <TabPane ecId={`${this && this.props && this.props.ecId || ''}_TabPane@9lzgmz`} tab={getLabel(518329, "批量处理")} key="2">
                            <div className="wf-scanworkflow-tab-content-wrap">
                                <textarea id="scan_textarea" className="wf-scanworkflow-tab-textArea"></textarea>
                                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ohphsq`} type="primary" onClick={this.onClick}>{getLabel(127766, "打开流程")}</Button>
                            </div>
                            <div className="wf-scanworkflow-tip">{getLabel(128509, "使用扫描枪扫描二维码或条形码后，点击【打开流程】可批量打开所有条码对应的流程")}</div>
                        </TabPane>
                    </Tabs>
                </div>
            </WeaTop>
        )
    }
}
